package com.xdf.data_transmitter.service;

import com.xdf.data_transmitter.dao.cloud.PlanReportMapper;
import com.xdf.data_transmitter.model.GCjflowbilldetailprocess;
import com.xdf.data_transmitter.model.PlanReport;
import com.xdf.data_transmitter.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlanReportServiceImpl implements PlanReportService {
    @Autowired
    private PlanReportMapper dao;
    @Autowired
    private GPrintingdyeingnameService printingDyeService;
    @Autowired
    private GCjflowbilldetailprocessService processYield;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return dao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(PlanReport record) {
        return dao.insert(record);
    }

    @Override
    public int insertSelective(PlanReport record) {
        return dao.insertSelective(record);
    }

    @Override
    public PlanReport selectByPrimaryKey(Long id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PlanReport record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PlanReport record) {
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public int updateStart(String mill, String vat_no) {
        return dao.updateStart(mill,vat_no);
    }

    @Override
    public List<PlanReport> selectMillAll(String mill) {
        return dao.selectMillAll(mill);
    }

    @Override
    public int saveBatch(List<PlanReport> list) {
        return dao.saveBatch(list);
    }

    /**
     * 更新云端本厂在表 plan_report 每个缸单的 isStart 状态
     * 至少有一个工序刷卡则认为开始了，设置为1，相反设置为0
     * @return
     */
    @Override
    public int refreshStart() {
        String mill = printingDyeService.getMillName();
        if( mill == null || mill.length()<=0 ) throw new BusinessException("没有配置染厂名称！");
        // 在表plan_report中查询获取指定染厂的所有记录
        List<PlanReport> list = dao.selectMillAll(mill);
        if( list == null || list.size()<=0 ) throw new BusinessException("表plan_report没有染厂" + mill + "任何记录！");
        for(int i=0;i<list.size();i++){
            List<GCjflowbilldetailprocess> yields = processYield.selectByVatnum(list.get(i).getVat_no());
            if( yields == null|| yields.size()<=0 )
                list.get(i).setIsStart(0);
            else
                list.get(i).setIsStart(1);
        }

        int ret = saveBatch(list);// 批量更新
        return list.size();
    }
}
